//
//  ViewController.swift
//  BMWShareDemo
//
//  Created by Li Shengbing, (Shengbing.Li@partner.bmw.com) on 2019/6/10.
//  Copyright © 2019年 Li Shengbing, (Shengbing.Li@partner.bmw.com). All rights reserved.
//

import UIKit
import BMWShareSDK


class ViewController: UIViewController, WXApiDelegate {
    
    /// weixin use property
    var WXPlatform: WXScene? = WXSceneTimeline
    
    /// qq use property
    var QQPlatform: ShareQQPlatform? = ShareQQPlatform.qqAnyWhere
    var shareDestType: ShareDestType? = ShareDestTypeQQ

    var numberOfLines: Int = 1
    var style: HUDColorStyle? = .black
    override func viewDidLoad() {
        view.backgroundColor = UIColor.hexStringToColor(hexString: "#D7D8D9")
        navigationItem.title = "分享平台接口测试"
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "share"), style: UIBarButtonItem.Style.plain, target: self, action: #selector(beginShare))
    }

    @IBAction func numberOfLinesSegment(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            self.numberOfLines = 1
        case 1:
            self.numberOfLines = 2
        default:
            break
        }
    }
    
    
    @IBAction func styleSegment(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            self.style = .black
        case 1:
            self.style = .white
        default:
            break
        }
    }
    
    
    
    @objc fileprivate func beginShare() {
        let shareView = BMWShareView(containerView: self.view)
        shareView.bmwShareViewAlpha = 1.0
        shareView.numberOfLines = self.numberOfLines
        shareView.sharePlatforms = [.weixin, .pengyouquan, .qq, .sms, .copy]
        //shareView.isHiddenIfNotInstallPlatform = true
        shareView.shareContents = [
            BMWShareManager.ContentKey.text: "上海市浦东新区陆家嘴上海中心大厦",
            BMWShareManager.ContentKey.url: "https://www.baidu.com"
        ]/*
        shareView.shareContents = [
            BMWShareManager.ContentKey.text: "标题",
            BMWShareManager.ContentKey.description: "详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息详情描述信息.",
            BMWShareManager.ContentKey.image: UIImage(named: "shareImage")!,
            BMWShareManager.ContentKey.url: "https://www.baidu.com"
        ]*/
        shareView.showShareView(.window)
        /// 获取分享回调
        shareView.getShareComplete { (name, indexPath, result) in
            print("点击返回的事件:indexpath=\(indexPath), title=\(name), 分享结果result=\(result)")
            //let message = "\(name): \(result)"
            //self.showAlertViewController(leftHandler: nil, rightHandler: nil, mainTitle: message, message: nil, leftTitle: "OK", rightTitle: nil)
            //self.showHud(title: result)
            self.showHud(title: result, style: self.style, complete: nil)
        }
        
        shareView.beginShareViewComplete { (title, indexPath) in
            if title == .sms {
               print("开始分享回调=\(title), indexPath=\(indexPath)")
            }
        }
        
        shareView.cancelShareViewComplete {
            print("取消按钮回调")
        }
        
        shareView.endShareViewComplete { (title, indexPath, result) in
            print("分享结束=title=\(title), indexPath=\(indexPath), result=\(result)")
            self.showHud(title: result, style: self.style, complete: nil)
        }
        
    }
    
    
    func topViewController() -> UIViewController? {
        var presentingViewController = UIApplication.shared.keyWindow?.rootViewController
        while let presentedViewController = presentingViewController?.presentedViewController {
            presentingViewController = presentedViewController
        }
        return presentingViewController
    }
}




/********************************************************/
/*******************  微信测试  **************************/
/********************************************************/


// MARK: - Share Weixin test case.
extension ViewController {
    @IBAction func weixinSegment(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            print("朋友圈")
            WXPlatform = WXSceneTimeline
        case 1:
            print("会话")
            WXPlatform = WXSceneSession
        case 2:
            print("收藏")
            WXPlatform = WXSceneFavorite
        default:
            break
        }
    }
    
    @IBAction func wxTypeSegment(_ sender: UISegmentedControl) {
        let image = UIImage(named: "shareImage")!
        
        switch sender.selectedSegmentIndex {
        case 0:
            let contents = [BMWShareManager.ContentKey.text: "分享文字"]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.weixin(WXPlatform!)) { (result) in
                print("微信|分享文字|结果: \(result)")
            }
            
        case 1:
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "描述",
                BMWShareManager.ContentKey.image: image
                ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.weixin(WXPlatform!)) { (result) in
                print("微信|分享图片|结果: \(result)")
            }
            
        case 2:
            let url = "https://music.163.com/#/song?id=30260225&userid=314131372"
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "描述",
                BMWShareManager.ContentKey.image: image,
                BMWShareManager.ContentKey.url: url
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.weixin(WXPlatform!)) { (result) in
                print("微信|分享URL|结果: \(result)")
            }
            
        case 3:
            let url = "http://mini.eastday.com/a/190611082038567.html?qid=02157"
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "描述",
                BMWShareManager.ContentKey.image: image,
                BMWShareManager.ContentKey.url: url
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.weixin(WXPlatform!)) { (result) in
                print("微信|分享音乐|结果: \(result)")
            }
            
        case 4:
            let filePath = Bundle.main.path(forResource: "aaa", ofType: "pdf") ?? ""
            let url = URL(fileURLWithPath: filePath)
            guard let fileData = try? Data(contentsOf: url) else { return }
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "描述",
                BMWShareManager.ContentKey.image: image,
                BMWShareManager.ContentKey.fileData: fileData
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.weixin(WXPlatform!)) { (result) in
                print("微信|分享文件|结果: \(result)")
            }
           
        case 5:
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "描述",
                BMWShareManager.ContentKey.image: image,
                BMWShareManager.ContentKey.videoUrl: "http://v.vqida.com/181121/xiamen02.mp4",
                BMWShareManager.ContentKey.videoLowBandUrl: "http://v.vqida.com/181121/xiamen02.mp4"
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.weixin(WXPlatform!)) { (result) in
                print("微信|分享视频|结果: \(result)")
            }
            
        default:
            break
        }
    }
}




/********************************************************/
/*******************  QQ测试  **************************/
/********************************************************/


// MARK: - Share QQ test case.
extension ViewController {
    @IBAction func qqSegment(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            shareDestType = ShareDestTypeQQ
        case 1:
            shareDestType = ShareDestTypeTIM
        default:
            break
        }
    }
    
    @IBAction func qqToWhereSegment(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            QQPlatform = ShareQQPlatform.qqAnyWhere
        case 1:
            QQPlatform = ShareQQPlatform.favirites
        case 2:
            QQPlatform = ShareQQPlatform.dataline
        case 3:
            QQPlatform = ShareQQPlatform.firbidZone
        case 4:
            QQPlatform = ShareQQPlatform.zoneOnly
        default:
            break
        }
    }
    
    @IBAction func qqTypeSegment(_ sender: UISegmentedControl) {
        
        let image = UIImage(named: "shareImage")!
        switch sender.selectedSegmentIndex {
        case 0:
            let contents = [BMWShareManager.ContentKey.text: "分享文字"]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.qq(QQPlatform!, shareDestType!)) { (result) in
                print("QQ|分享文字|结果: \(result)")
            }
        case 1:
            
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "我是描述",
                BMWShareManager.ContentKey.image: image
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.qq(QQPlatform!, shareDestType!)) { (result) in
                print("QQ|分享图片|结果: \(result)")
            }
            
        case 2:
            /// http://v.vqida.com/181121/xiamen02.mp4
            /// http://mini.eastday.com/a/190611082038567.html?qid=02157
            let url = "http://v.vqida.com/181121/xiamen02.mp4"
            let previewImageUrl = "http://imgcache.qq.com/music/photo/mid_album_300/V/E/000J1pJ50cDCVE.jpg"
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "我是描述",
                BMWShareManager.ContentKey.url: url,
                BMWShareManager.ContentKey.previewImageUrl: previewImageUrl
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.qq(QQPlatform!, shareDestType!)) { (result) in
                print("QQ|分享URL|结果: \(result)")
            }
            
        case 3:
            let audioUrl = "http://mini.eastday.com/a/190611082038567.html?qid=02157"
            let previewImageUrl = "http://imgcache.qq.com/music/photo/mid_album_300/V/E/000J1pJ50cDCVE.jpg"
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "我是描述",
                BMWShareManager.ContentKey.audioUrl: audioUrl,
                BMWShareManager.ContentKey.previewImageUrl: previewImageUrl
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.qq(QQPlatform!, shareDestType!)) { (result) in
                print("QQ|分享音频|结果: \(result)")
            }
        case 4:
            let videoUrl = "http://v.vqida.com/181121/xiamen02.mp4"
            let previewImageUrl = "http://imgcache.qq.com/music/photo/mid_album_300/V/E/000J1pJ50cDCVE.jpg"
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "我是描述",
                BMWShareManager.ContentKey.videoUrl: videoUrl,
                BMWShareManager.ContentKey.previewImageUrl: previewImageUrl
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.qq(QQPlatform!, shareDestType!)) { (result) in
                print("QQ|分享视频|结果: \(result)")
            }
            
        default:
            break
        }
    }
    
    @IBAction func qq2Segment(_ sender: UISegmentedControl) {
        
        let image = UIImage(named: "shareImage")!
        switch sender.selectedSegmentIndex {
        case 0:
            let filePath = Bundle.main.path(forResource: "aaa", ofType: "pdf") ?? ""
            let url = URL(fileURLWithPath: filePath)
            guard let fileData = try? Data(contentsOf: url) else { return }
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "描述",
                BMWShareManager.ContentKey.previewImage: image,
                BMWShareManager.ContentKey.fileData: fileData
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.qq(ShareQQPlatform.dataline, shareDestType!)) { (result) in
                print("微信|分享文件|结果: \(result)")
            }
        default:
            break
        }
    }
}




/********************************************************/
/*******************  微博测试  **************************/
/********************************************************/


// MARK: - Share weibo test case.
extension ViewController {
    @IBAction func weiboSegment(_ sender: UISegmentedControl) {}
    
    @IBAction func weiboTypeSegment(_ sender: UISegmentedControl) {
        
        let image = UIImage(named: "shareImage")!
        switch sender.selectedSegmentIndex {
        case 0:
            let contents = [BMWShareManager.ContentKey.text: "分享文字"]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.weibo) { (result) in
                print("微博|分享文字|结果: \(result)")
            }
            
        case 1:
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "描述",
                BMWShareManager.ContentKey.image: image
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.weibo) { (result) in
                print("微博|分享图片|结果: \(result)")
            }
            
        case 2:
            /// 大牌视频.mp4
            let url = "http://www.jianshu.com/u/2846c3d3a974"
            let contents: [BMWShareManager.ContentKey: Any] = [
                BMWShareManager.ContentKey.text: "分享文字",
                BMWShareManager.ContentKey.description: "描述",
                BMWShareManager.ContentKey.url: url
            ]
            BMWShareManager.share(contents: contents, platform: BMWShareManager.Platform.weibo) { (result) in
                print("微博|分享URL|结果: \(result)")
            }
           
        case 3:
            print(sender.titleForSegment(at: sender.selectedSegmentIndex) ?? "")
        case 4:
            //let vedioUrl = "https://music.163.com/#/song?id=30260225&userid=314131372"
            let _ = Bundle.main.path(forResource: "大牌视频.mp4", ofType: nil)
            //BMWShareManager.willShareToWeibo(type: .url(url: vedioUrl, text: "多媒体的标题", description: "多媒体的描", thumbImage: image))
            //BMWShareManager.willShareToWeibo(type: .vedio(text: "我会标题", description: "我是描述", vedioUrl: path!))
        default:
            break
        }
    }
}

extension UIViewController {
    
    // handler: ((UIAlertAction) -> Void)? = nil
    
    /// show alert message
    ///
    /// - Parameters:
    ///   - leftHandler: callback left button event to outside.
    ///   - rightHandler: callback right button event to outside.
    ///   - mainTitle: main title
    ///   - message: message
    ///   - leftTitle: left button title
    ///   - rightTitle: right button title
    func showAlertViewController(leftHandler: ((UIAlertAction) -> Void)? = nil, rightHandler: ((UIAlertAction) -> Void)? = nil, mainTitle: String? = nil, message: String? = nil, leftTitle: String? = nil, rightTitle: String? = nil) {
        
        let alertVc = UIAlertController(title: mainTitle ?? "\"请输入一个标题\"", message: message, preferredStyle: UIAlertController.Style.alert)
        let leftAction = UIAlertAction(title: leftTitle ?? "\"缺少一个左边按钮标题\"", style: .default, handler: leftHandler)
        let rightAction = UIAlertAction(title: rightTitle ?? "\"缺少一个右边按钮标题\"", style: .default, handler: rightHandler)
        if let leftText = leftTitle, !leftText.isEmpty {
            alertVc.addAction(leftAction)
        }
        if let rightText = rightTitle, !rightText.isEmpty {
            alertVc.addAction(rightAction)
        }
        present(alertVc, animated: true, completion: nil)
    }
}
